package com.example.xiaoxueqixiangmu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;


public class MyFragmentActivity extends FragmentActivity implements View.OnClickListener {
    public static final int TAB_TICKET=0;
    public static final int TAB_ORDER=1;
    public static final int TAB_MINE=2;

    private ViewPager viewPager;
    private RadioButton btn_ticket,btn_order,btn_mine;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_fragment);
        initView();


    }

    @SuppressLint("WrongViewCast")
    private void initView(){
        viewPager=findViewById(R.id.fragment_viewpager);
        btn_ticket=findViewById(R.id.ticket_tab);
        btn_order=findViewById(R.id.order_tab);
        btn_mine=findViewById(R.id.mine_tab);
        btn_ticket.setOnClickListener(this);
        btn_order.setOnClickListener(this);
        btn_mine.setOnClickListener(this);

        MyFragmentAdapter adapter=new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
              switch (position){
                  case TAB_TICKET:
                      btn_ticket.setChecked(true);
                      break;
                  case TAB_ORDER:
                      btn_order.setChecked(true);
                      break;
                  case TAB_MINE:
                      btn_mine.setChecked(true);
                      break;
                  default:
                      break;

              }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ticket_tab:
                viewPager.setCurrentItem(TAB_TICKET);
                break;
            case R.id.order_tab:
                viewPager.setCurrentItem(TAB_ORDER);
                break;
            case R.id.mine_tab:
                viewPager.setCurrentItem(TAB_MINE);
                break;
        }

    }
}